
package com.wwy.monitor.service.impl;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.common.PageResult;
import com.wwy.monitor.mapper.tddl.MonitorLogMapper;
import com.wwy.monitor.mapper.tddl.MonitorTaskMapper;
import com.wwy.monitor.model.tddl.dto.MonitorTaskDTO;
import com.wwy.monitor.model.tddl.query.MonitorLogQuery;
import com.wwy.monitor.model.tddl.vo.MonitorLogVO;
import com.wwy.monitor.service.MonitorLogService;
import com.wwy.monitor.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author long.Wanheng
 * @version V1.00
 * @createDate 2019/10/30
 */
@Service(value = "monitorLogService")
public class MonitorLogServiceImpl implements MonitorLogService {
    @Autowired
    private MonitorLogMapper monitorLogMapper;
    @Autowired
    private MonitorTaskMapper monitorTaskMapper;

    @Override
    public PageResult queryMonitorLog(MonitorLogQuery query) {
        PageResult result = new PageResult();
        if (query.getTaskId() != null) {
            MonitorTaskDTO taskVo = monitorTaskMapper.selectById(query.getTaskId());
            if (taskVo != null) {
                query.setDs(DateUtil.getNowDateShort(taskVo.getCreateTime(), DateUtil.SHOW_PATTERN_DATE));
            } else {
                result.setCurrentPage(query.getPageNum());
                result.setPages(query.getPageNum());
                result.setPageSize(query.getPageSize());
                result.setTotalCount(0);
                result.setDatas(null);
                return result;
            }
        } else {
            verifyParam(query);
            query.setStartDate(new Date(query.getStartTime()));
            query.setEndDate(new Date(query.getEndTime()));
//åè¡¨èå´å­æ®µ
            query.setStartDsTime(DateUtil.getNowDateShort(query.getStartDate(), DateUtil.SHOW_PATTERN_DATE));
            query.setEndDsTime(DateUtil.getNowDateShort(query.getEndDate(), DateUtil.SHOW_PATTERN_DATE));
        }
//å¼å¯åé¡µ
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();
        Integer offset = (pageNum - 1) * pageSize;
        query.setPageNum(offset);
        query.setPageSize(pageSize);
        int dataCount = monitorLogMapper.queryMonitorLogCount(query);
        int pages = (dataCount + pageSize - 1) / pageSize;
        List<MonitorLogVO> data = monitorLogMapper.queryMonitorLog(query);
        result.setCurrentPage(pageNum);
        result.setPages(pages);
        result.setPageSize(pageSize);
        result.setTotalCount(dataCount);
        result.setDatas(data);
        return result;
    }

    private void verifyParam(MonitorLogQuery monitorLogQuery) {
        if (null == monitorLogQuery) {
            throw new MessageException("éæ³参数");
        }
        if (null == monitorLogQuery.getStartTime() || null == monitorLogQuery.getEndTime()) {
            if (null == monitorLogQuery.getDs()) {
                throw new MessageException("æ¶é´èå´ä¸è½ä¸ºç©º");
            }
        }
    }
}